// pages/comment/comment.js
let rows = [
  {
    head: '../../static/image/head1.jpg',
    name: '网易火星网友',
    addr: '福建厦门市',
    phone: 'VIVO',
    date: '6小时前',
    topCounts: '1.8w',
    contents: '30万升降也经不起这么多软件来回测试'
  },
  {
    head: '../../static/image/head2.jpg',
    name: '网易火星网友',
    addr: '上海市',
    phone: 'VIVO',
    date: '6小时前',
    topCounts: 8564,
    contents: '30万升降也经不起这么多软件来回测试来回测试来回测试来回测试'
  },
  {
    head: '../../static/image/head3.jpg',
    name: '网易火星网友',
    addr: '北京市',
    phone: 'VIVO',
    date: '6小时前',
    topCounts: 1235,
    contents: '30万升降也经不起这么多软件来回测试也经不起这么多软件也经不起这么多软件'
  }
]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page:0,
    bottomLoadStatus:'',
    title:''
  },
  //上拉加载
  onReachBottom(){
    let that = this;
    let append = [];
    if (that.data.bottomLoadStatus === 'loading') return;
    that.setData({ bottomLoadStatus: "loading" });
    setTimeout(() => {
      if (that.data.page < 1) append = rows;
      if (append.length > 0) {
        that.setData({ list: that.data.list.concat(append) });
        that.setData({ page: that.data.page += 1 });
        that.setData({ bottomLoadStatus: "loaded" });
      } else {
        that.setData({ bottomLoadStatus: "nodata" });
      }
    },500)
  },
  onPageScroll() {
    let that = this;
    wx.createSelectorQuery().selectViewport().scrollOffset(function (res) {
      let _change = false;
      let _title = "";
      if (res.scrollTop >= 42 && that.data.title === '') {
        _title = "热门跟帖";
        _change = true;
      }
      if (res.scrollTop < 42 && that.data.title !== '') {
        _change = true;
      }
      if (_change) {
        wx.setNavigationBarTitle({
          title: _title
        })
        that.setData({
          title: _title
        })
      }
    }).exec()
  },

  onPullDownRefresh(){
    let that = this;
    setTimeout(() => {
      (function _set(arr, cb) {
        let _arr = arr.concat(rows);
        if (_arr.length > 10) return cb(_arr);
        _set(_arr, cb);
      })([], (d) => {
        rows = d;
        that.setData({ list: rows });
        that.setData({ page: 0 });
        wx.stopPullDownRefresh()
      })
    },1000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    (function _set(arr,cb){
      let _arr = arr.concat(rows);
      if (_arr.length > 10) return cb(_arr);
      _set(_arr, cb);
    })([],(d) =>{
        rows = d;
        this.setData({ list: rows })
    })
  }
})